﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using CRM.Entity;

using CRM.BLL;
using DP.Web.UI.Controls;
using DP.Web.UI;
using DP.Common;
using System.Data;

namespace CRM.Web.VehicleManage
{
    public partial class CustomerCare :Common.PageBase
    {

        public string ModifyUrl = "Modify.aspx?VEHICLEID={0}";
        T_SALEVEHICLE_BLL bll = new T_SALEVEHICLE_BLL();
        private T_SMS_SEND_BLL smsSendBll = new T_SMS_SEND_BLL();
        //SqlServerDAL<T_SALEVEHICLE_Entity> dal = new SqlServerDAL<T_SALEVEHICLE_Entity>();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            gvList.PageSize = 100;
            PageHelper.SetPrePageUrl(this.Page);
            //PageHelper.SetPopCalender(this.Page, txtOFFLINEDATEBegin);
            //PageHelper.SetPopCalender(this.Page, txtOFFLINEDATEEnd);
            //PageHelper.SetPopCalender(this.Page, txtFIRSTSALESDATEBegin);
            //PageHelper.SetPopCalender(this.Page, txtFIRSTSALESDATEEnd);
            //PageHelper.SetPopCalender(this.Page, txtMANUFACTUREDATEBegin);
            //PageHelper.SetPopCalender(this.Page, txtMANUFACTUREDATEEnd);
            PageHelper.SetPopCalender(this.Page, txtPURCHASEDATEBegin);
            PageHelper.SetPopCalender(this.Page, txtPURCHASEDATEEnd);
            //PageHelper.SetPopCalender(this.Page, txtCREATETIMEBegin);
            //PageHelper.SetPopCalender(this.Page, txtCREATETIMEEnd);
            //PageHelper.SetPopCalender(this.Page, txtLASTMODIFYTIMEBegin);
            //PageHelper.SetPopCalender(this.Page, txtLASTMODIFYTIMEEnd);
            //PageHelper.SetPopCalender(this.Page, txtLASTSALEDATEBegin);
            //PageHelper.SetPopCalender(this.Page, txtLASTSALEDATEEnd);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                GetUrlQueryToControl();
                gvDataBinding();
            }
        }

        #region 页面初始化数据绑定



        #endregion

        /// <summary>
        /// 获取 URL 上的参数存入当页面 控件中。
        /// </summary>
        private void GetUrlQueryToControl()
        {
            PageHelper.GetUrlQueryToControl(this.Page);
        }

        /// <summary>
        /// 生成查询条件字符串
        /// </summary>
        private StringBuilder GenerationSQLString()
        {
            StringBuilder sql = new StringBuilder();
            if (!String.IsNullOrEmpty(Request.QueryString["VEHICLEID"]))
            {
                sql.Append(" and VEHICLEID like '%" + Request.QueryString["VEHICLEID"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VINCODE"]))
            {
                sql.Append(" and VINCODE like '%" + Request.QueryString["VINCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PRODUCTCODE"]))
            {
                sql.Append(" and PRODUCTCODE like '%" + Request.QueryString["PRODUCTCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BRANDCODE"]))
            {
                sql.Append(" and BRANDCODE like '%" + Request.QueryString["BRANDCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VEHICLETYPECODE"]))
            {
                sql.Append(" and VEHICLETYPECODE like '%" + Request.QueryString["VEHICLETYPECODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SERIESCODE"]))
            {
                sql.Append(" and SERIESCODE like '%" + Request.QueryString["SERIESCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MODELSCODE"]))
            {
                sql.Append(" and MODELSCODE like '%" + Request.QueryString["MODELSCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENGINEMODELCODE"]))
            {
                sql.Append(" and ENGINEMODELCODE like '%" + Request.QueryString["ENGINEMODELCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENGINEMANUFACTURERSCODE"]))
            {
                sql.Append(" and ENGINEMANUFACTURERSCODE like '%" + Request.QueryString["ENGINEMANUFACTURERSCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BRAND"]))
            {
                sql.Append(" and BRAND like '%" + Request.QueryString["BRAND"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VEHICLETYPE"]))
            {
                sql.Append(" and VEHICLETYPE like '%" + Request.QueryString["VEHICLETYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SERIES"]))
            {
                sql.Append(" and SERIES like '%" + Request.QueryString["SERIES"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MODELS"]))
            {
                sql.Append(" and MODELS like '%" + Request.QueryString["MODELS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENGINEMODEL"]))
            {
                sql.Append(" and ENGINEMODEL like '%" + Request.QueryString["ENGINEMODEL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENGINEMANUFACTURERS"]))
            {
                sql.Append(" and ENGINEMANUFACTURERS like '%" + Request.QueryString["ENGINEMANUFACTURERS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENGINENUMBER"]))
            {
                sql.Append(" and ENGINENUMBER like '%" + Request.QueryString["ENGINENUMBER"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FRAMENUMBER"]))
            {
                sql.Append(" and FRAMENUMBER like '%" + Request.QueryString["FRAMENUMBER"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["RANGE"]))
            {
                sql.Append(" and RANGE like '%" + Request.QueryString["RANGE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["OFFLINEDATEBegin"]))
            {
                sql.Append(" and  " + bll._dal.GetBeginDateString("OFFLINEDATE", Request.QueryString["OFFLINEDATEBegin"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["OFFLINEDATEEnd"]))
            {
                sql.Append(" and  " + bll._dal.GetEndDateString("OFFLINEDATE", Request.QueryString["OFFLINEDATEEnd"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FIRSTSALESDATEBegin"]))
            {
                sql.Append(" and  " + bll._dal.GetBeginDateString("FIRSTSALESDATE", Request.QueryString["FIRSTSALESDATEBegin"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FIRSTSALESDATEEnd"]))
            {
                sql.Append(" and  " + bll._dal.GetEndDateString("FIRSTSALESDATE", Request.QueryString["FIRSTSALESDATEEnd"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERID"]))
            {
                sql.Append(" and CUSTOMERID = '" + Request.QueryString["CUSTOMERID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERCODE"]))
            {
                sql.Append(" and CUSTOMERCODE like '%" + Request.QueryString["CUSTOMERCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERNAME"]))
            {
                sql.Append(" and CUSTOMERNAME like '%" + Request.QueryString["CUSTOMERNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MANUFACTUREDATEBegin"]))
            {
                sql.Append(" and  " + bll._dal.GetBeginDateString("MANUFACTUREDATE", Request.QueryString["MANUFACTUREDATEBegin"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MANUFACTUREDATEEnd"]))
            {
                sql.Append(" and  " + bll._dal.GetEndDateString("MANUFACTUREDATE", Request.QueryString["MANUFACTUREDATEEnd"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PURCHASEDATEBegin"]))
            {
                sql.Append(" and  " + bll._dal.GetBeginDateString("PURCHASEDATE", Request.QueryString["PURCHASEDATEBegin"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PURCHASEDATEEnd"]))
            {
                sql.Append(" and  " + bll._dal.GetEndDateString("PURCHASEDATE", Request.QueryString["PURCHASEDATEEnd"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MARKETDEPARTMENTID"]))
            {
                sql.Append(" and MARKETDEPARTMENTID = '" + Request.QueryString["MARKETDEPARTMENTID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MARKETDEPARTMENTCODE"]))
            {
                sql.Append(" and MARKETDEPARTMENTCODE like '%" + Request.QueryString["MARKETDEPARTMENTCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MARKETDEPARTMENTNAME"]))
            {
                sql.Append(" and MARKETDEPARTMENTNAME like '%" + Request.QueryString["MARKETDEPARTMENTNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEALERCODE"]))
            {
                sql.Append(" and DEALERCODE like '%" + Request.QueryString["DEALERCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEALERNAME"]))
            {
                sql.Append(" and DEALERNAME like '%" + Request.QueryString["DEALERNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PURCHASEMETHOD"]))
            {
                sql.Append(" and PURCHASEMETHOD like '%" + Request.QueryString["PURCHASEMETHOD"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ISSUBSIDIES"]))
            {
                sql.Append(" and ISSUBSIDIES like '%" + Request.QueryString["ISSUBSIDIES"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SALESTYPE"]))
            {
                sql.Append(" and SALESTYPE like '%" + Request.QueryString["SALESTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CONTACTNAME"]))
            {
                sql.Append(" and CONTACTNAME like '%" + Request.QueryString["CONTACTNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CONTACTTEL"]))
            {
                sql.Append(" and CONTACTTEL like '%" + Request.QueryString["CONTACTTEL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VEHICLESTATUS"]))
            {
                sql.Append(" and VEHICLESTATUS like '%" + Request.QueryString["VEHICLESTATUS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PRODUCTID"]))
            {
                sql.Append(" and PRODUCTID = '" + Request.QueryString["PRODUCTID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CFGPRODUCTCODE"]))
            {
                sql.Append(" and CFGPRODUCTCODE like '%" + Request.QueryString["CFGPRODUCTCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEBegin"]))
            {
                sql.Append(" and  " + bll._dal.GetBeginDateString("CREATETIME", Request.QueryString["CREATETIMEBegin"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEEnd"]))
            {
                sql.Append(" and  " + bll._dal.GetEndDateString("CREATETIME", Request.QueryString["CREATETIMEEnd"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLEID"]))
            {
                sql.Append(" and CREATEPEOPLEID = '" + Request.QueryString["CREATEPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLE"]))
            {
                sql.Append(" and CREATEPEOPLE like '%" + Request.QueryString["CREATEPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLEID"]))
            {
                sql.Append(" and LASTMODIFYPEOPLEID = '" + Request.QueryString["LASTMODIFYPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLE"]))
            {
                sql.Append(" and LASTMODIFYPEOPLE like '%" + Request.QueryString["LASTMODIFYPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEBegin"]))
            {
                sql.Append(" and  " + bll._dal.GetBeginDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEBegin"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEEnd"]))
            {
                sql.Append(" and  " + bll._dal.GetBeginDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEEnd"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["REMARK"]))
            {
                sql.Append(" and REMARK like '%" + Request.QueryString["REMARK"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONID"]))
            {
                sql.Append(" and BUSINESSDIVISIONID = '" + Request.QueryString["BUSINESSDIVISIONID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONNAME"]))
            {
                sql.Append(" and BUSINESSDIVISIONNAME like '%" + Request.QueryString["BUSINESSDIVISIONNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONCODE"]))
            {
                sql.Append(" and BUSINESSDIVISIONCODE like '%" + Request.QueryString["BUSINESSDIVISIONCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CARNUMBER"]))
            {
                sql.Append(" and CARNUMBER like '%" + Request.QueryString["CARNUMBER"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CHASSISNO"]))
            {
                sql.Append(" and CHASSISNO like '%" + Request.QueryString["CHASSISNO"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SERIALNUMBER"]))
            {
                sql.Append(" and SERIALNUMBER like '%" + Request.QueryString["SERIALNUMBER"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["GEARTYPE"]))
            {
                sql.Append(" and GEARTYPE like '%" + Request.QueryString["GEARTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["GEARCODE"]))
            {
                sql.Append(" and GEARCODE like '%" + Request.QueryString["GEARCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LICENSECODE"]))
            {
                sql.Append(" and LICENSECODE like '%" + Request.QueryString["LICENSECODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTSALEDATEBegin"]))
            {
                sql.Append(" and  " + bll._dal.GetBeginDateString("LASTSALEDATE", Request.QueryString["LASTSALEDATEBegin"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTSALEDATEEnd"]))
            {
                sql.Append(" and  " + bll._dal.GetEndDateString("LASTSALEDATE", Request.QueryString["LASTSALEDATEEnd"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["INVOICECODE"]))
            {
                sql.Append(" and INVOICECODE like '%" + Request.QueryString["INVOICECODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SALESPRICE"]))
            {
                sql.Append(" and SALESPRICE like '%" + Request.QueryString["SALESPRICE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PRICE"]))
            {
                sql.Append(" and PRICE like '%" + Request.QueryString["PRICE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["COLOR"]))
            {
                sql.Append(" and COLOR like '%" + Request.QueryString["COLOR"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BULLETINCODE"]))
            {
                sql.Append(" and BULLETINCODE like '%" + Request.QueryString["BULLETINCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MODIFYCOUNT"]))
            {
                sql.Append(" and MODIFYCOUNT like '%" + Request.QueryString["MODIFYCOUNT"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["QUICKCODE"]))
            {
                sql.Append(" and QUICKCODE like '%" + Request.QueryString["QUICKCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SIMPLELICENCE"]))
            {
                sql.Append(" and SIMPLELICENCE like '%" + Request.QueryString["SIMPLELICENCE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PURPOSE"]))
            {
                sql.Append(" and PURPOSE like '%" + Request.QueryString["PURPOSE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SUPPLYCODE"]))
            {
                sql.Append(" and SUPPLYCODE like '%" + Request.QueryString["SUPPLYCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SUPPLYNAME"]))
            {
                sql.Append(" and SUPPLYNAME like '%" + Request.QueryString["SUPPLYNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SERIALMENO"]))
            {
                sql.Append(" and SERIALMENO like '%" + Request.QueryString["SERIALMENO"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STATIONCODE"]))
            {
                sql.Append(" and STATIONCODE like '%" + Request.QueryString["STATIONCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["EMISSIONS"]))
            {
                sql.Append(" and EMISSIONS like '%" + Request.QueryString["EMISSIONS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STATIONNAME"]))
            {
                sql.Append(" and STATIONNAME like '%" + Request.QueryString["STATIONNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["OWNERSMOBILEPHONE"]))
            {
                sql.Append(" and OWNERSMOBILEPHONE like '%" + Request.QueryString["OWNERSMOBILEPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["OWNERSOFFICEPHONE"]))
            {
                sql.Append(" and OWNERSOFFICEPHONE like '%" + Request.QueryString["OWNERSOFFICEPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["OWNERSHOMEPHONE"]))
            {
                sql.Append(" and OWNERSHOMEPHONE like '%" + Request.QueryString["OWNERSHOMEPHONE"] + "%'");
            }
            if (!UserLoginInfo.IsAdministrator())
            {
                sql.Append(" and BUSINESSDIVISIONCODE = '" + _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim() + "'");
            }
            return sql;

        }

        /// <summary>
        /// 页面数据绑定
        /// </summary>
        private void gvDataBinding()
        {
            #region 查询条件字符串
            StringBuilder sql = GenerationSQLString();
            #endregion

            List<T_SALEVEHICLE_Entity> list;

            //获取总的记录条数
            gvList.RecordCount = (int)bll.GetCount(sql.ToString());

            list = bll.GetList(sql.ToString(), gvList.CurrentPageIndex * gvList.PageSize, gvList.PageSize, gvList.SortConditions);

            gvList.DataSource = list;
            gvList.DataKeyNames = new string[] { "VEHICLEID", "OWNERSMOBILEPHONE" };
            gvList.DataBind();

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            PageHelper.GetControlToUrlQuery(this.Page, ref url);
            Response.Redirect(url);
        }

        protected void btnSendSMS_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if (btn == null) return;

            switch (btn.ID)
            {
                case "btnSelectedSendSMS":
                    {
                        SendSMSSelected();
                    }
                    break;
                case "btnAllSendSMS":
                    {
                        SendSMSAll();
                    }
                    break;
            }
            //string url = HttpContext.Current.Request.Url.AbsolutePath;
            //PageHelper.GetControlToUrlQuery(this.Page, ref url);
            //Response.Redirect(url);
        }


        private void SendSMSSelected()
        {
            if(String.IsNullOrEmpty(txtSMSContent.Text.Trim()))
            {
                PageHelper.ShowMessage(this.Page, "短信内容不能为空！");
                return;
            }

            string tels = GetSelected(gvList).ToString();
            if (String.IsNullOrEmpty(tels))
            {
                PageHelper.ShowMessage(this.Page, "短信号码不能为空！");
                return;
            }


            T_SMS_SEND_Entity smsSendEntity = new T_SMS_SEND_Entity();
            smsSendEntity.BODYS = txtSMSContent.Text;
            smsSendEntity.BUSINESSDIVISIONCODE = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
            smsSendEntity.BUSINESSDIVISIONID = _userLoginInfo.UserEntity.BUSINESSDIVISIONID;
            smsSendEntity.BUSINESSDIVISIONNAME = _userLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
            smsSendEntity.TEL_TO = tels;
            smsSendEntity.CREATETIME = DateTime.Now;
            smsSendEntity.GHID = _userLoginInfo.UserEntity.USERID.ToString();
            smsSendEntity.SUBJECTS = "客户关怀群发";
            smsSendEntity.LEVELS = 1;
            smsSendBll.Insert(smsSendEntity);
        }

        private void SendSMSAll()
        {
            string fieldName = "OWNERSMOBILEPHONE";
            if (String.IsNullOrEmpty(txtSMSContent.Text.Trim()))
            {
                PageHelper.ShowMessage(this.Page, "短信内容不能为空！");
                return;
            }

            int pageSize = 100;
            StringBuilder sql = GenerationSQLString();
            int recordCount= (int)bll.GetCount(sql.ToString());
            if (recordCount == 0)
            {
                PageHelper.ShowMessage(this.Page, "记录不能为空！");
                return;
            }

            //List<T_SALEVEHICLE_Entity> list;
            T_SMS_SEND_Entity smsSendEntity;


            int pageCount = IntHelper.divideMax(recordCount, pageSize);
            int currentPageIndex = 0;
            try
            {
                DataTable dt = bll.GetDistinctTable(fieldName, sql.ToString());
                smsSendEntity = new T_SMS_SEND_Entity();
                smsSendEntity.BODYS = txtSMSContent.Text;
                smsSendEntity.BUSINESSDIVISIONCODE = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
                smsSendEntity.BUSINESSDIVISIONID = _userLoginInfo.UserEntity.BUSINESSDIVISIONID;
                smsSendEntity.BUSINESSDIVISIONNAME = _userLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
                smsSendEntity.CREATETIME = DateTime.Now;
                smsSendEntity.GHID = _userLoginInfo.UserEntity.USERID.ToString();
                smsSendEntity.SUBJECTS = "客户关怀群发";
                smsSendEntity.LEVELS = 1;

                DataView dv = new DataView(dt);
                DataTable dvt = dv.ToTable(true, fieldName);

                StringBuilder sbTel = new StringBuilder();
                string tel = string.Empty;
                for (int i = 0; i < dvt.Rows.Count; i++)
                {
                    tel = dvt.Rows[i][fieldName].ToString();
                    if (tel.Length == 11)
                    {
                        sbTel.Append(tel).Append(";");
                    }
                    if ((i + 1) % 100 == 0 || (i + 1) == dvt.Rows.Count)
                    {
                        smsSendEntity.TEL_TO = sbTel.ToString().TrimEnd(new char[] { ';' });
                        smsSendBll.Insert(smsSendEntity);
                        sbTel = new StringBuilder();
                    }
                }
                

                //for (int i = 0; i < pageCount; i++)
                //{
                //    list = bll.GetList(sql.ToString(), i * pageSize, pageSize, gvList.SortConditions);

                //    smsSendEntity = new T_SMS_SEND_Entity();
                //    smsSendEntity.BODYS = txtSMSContent.Text;
                //    smsSendEntity.BUSINESSDIVISIONCODE = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
                //    smsSendEntity.BUSINESSDIVISIONID = _userLoginInfo.UserEntity.BUSINESSDIVISIONID;
                //    smsSendEntity.BUSINESSDIVISIONNAME = _userLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
                //    smsSendEntity.TEL_TO = GetMobile(list).ToString();
                //    smsSendEntity.CREATETIME = DateTime.Now;
                //    smsSendEntity.GHID = _userLoginInfo.UserEntity.USERID.ToString();
                //    smsSendEntity.SUBJECTS = "客户关怀群发";
                //    smsSendEntity.LEVELS = 1;
                //    smsSendBll.Insert(smsSendEntity);
                //    currentPageIndex = i;

                //}
                PageHelper.ShowMessage(this.Page, "发送完成！");
            }
            catch (Exception ex)
            {
                PageHelper.ShowMessage(this.Page, String.Format("发送失败！发送到第{0}页，错误内容为：{1}", currentPageIndex, ex.Message));
            }
        }

        /// <summary>
        /// Gets the mobile.
        /// </summary>
        /// <param name="list">The list.</param>
        /// <returns></returns>
        private StringBuilder GetMobile(List<T_SALEVEHICLE_Entity> list)
        {
            string temp = string.Empty;
            StringBuilder sb = new StringBuilder();
            if (list.Count == 0) return sb;
            foreach (var en in list)
            {
                temp = en.OWNERSMOBILEPHONE.Trim();
                if (!String.IsNullOrEmpty(temp))
                {
                    if (sb.Length != 0)
                    {
                        sb.Append(";");
                    }
                    sb.Append(temp);
                }
            }
            return sb;
        }

        /// <summary>
        /// Gets the selected.
        /// </summary>
        /// <param name="gv">The gv.</param>
        /// <returns></returns>
        private StringBuilder GetSelected(GridView gv)
        {
            string temp = string.Empty;
            StringBuilder sbReturnValue = new StringBuilder();
            List<string> list = new List<string>();
            if (gv == null) return sbReturnValue;
            CheckBox cb;
            foreach (GridViewRow gvr in gv.Rows)
            {
                cb = gvr.FindControl("cbItem") as CheckBox;
                if (cb == null) continue;
                if (!cb.Checked) continue;
                temp = gv.DataKeys[gvr.RowIndex].Values["OWNERSMOBILEPHONE"].ToString();
                if (!String.IsNullOrEmpty(temp))
                {
                    if (temp.Length == 11)
                    {
                        if (!list.Exists(p => p.ToString() == temp))
                        {
                            list.Add(temp);
                        }
                    }
                }
            }
            foreach (string s in list)
            {
                if (sbReturnValue.Length != 0)
                {
                    sbReturnValue.Append(";");
                }
                sbReturnValue.Append(s);
            }
            return sbReturnValue;
        }

        #region GridView 事件
        /// <summary>
        /// GridView 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            T_SALEVEHICLE_Entity entity = new T_SALEVEHICLE_Entity();
            entity.VEHICLEID = StringHelper.ToInt((sender as GridView).DataKeys[e.RowIndex].Values[0].ToString());

            long rel = bll.Delete(entity);
            if (rel > 0)
            {
                Response.Redirect(HttpContext.Current.Request.RawUrl);
            }
            else
            {

            }

        }

        /// <summary>
        /// GridView 行数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        GridView gv = (sender as GridView);
                        for (int i = 0; i < gv.Columns.Count; i++)
                        {
                            TemplateField tf = gv.Columns[i] as TemplateField;
                            if (tf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (tf.HeaderText)
                                {
                                    case "操作":
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;
                                    default:
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;

                                }
                                #endregion
                            }

                            BoundField bf = gv.Columns[i] as BoundField;
                            if (bf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (bf.DataField)
                                {
                                    default:
                                        {

                                        }
                                        break;
                                }
                                #endregion
                            }
                        }
                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }

        /// <summary>
        /// GridView 行创建事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {

                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }
        #endregion
    }
}
